import React from 'react'
import { Route, Redirect } from 'react-router-dom'
import loginInfo from './loginInfo'

// 封装受保护的路由组件
export default function ProtectedRoute({ component: Component, ...rest }) { //children 
    if (loginInfo.isLogin) {
        return <Route {...rest} component={Component}></Route>
    } else {
        console.log('还没有授权，跳转登录');
        // 可以加参数 to={'login?prev=' + rest.to} 或
        // return <Redirect to={{
        //     pathname: '/login',
        //     search: '?prev=' + rest.path
        // }} />
        // 保存到state
        return <Redirect to={{
            pathname: '/login',
            state: rest.path
        }} />

    }
}
